package com.gimi.cloud.jimi.commonService.InterfacePlatform.impl;

import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.exception.MsgException;
import com.gimi.cloud.jimi.common.utils.DateUtil;
import com.gimi.cloud.jimi.common.utils.HttpClientUtil;
import com.gimi.cloud.jimi.commonService.InterfacePlatform.InterfacePlatformRequestService;
import com.gimi.cloud.jimi.commonService.InterfacePlatform.InterfacePlatformTokenService;
import com.gimi.cloud.jimi.dao.dao.moreInfo.MoreInfoLogDao;
import com.gimi.cloud.jimi.entity.app.moreInfo.MoreInfoLogEntity;
import org.apache.http.Header;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class InterfacePlatformRequestServiceImpl implements InterfacePlatformRequestService {
	@Autowired
	InterfacePlatformTokenService tokenService;
    @Autowired
	MoreInfoLogDao moreInfoLogDao;
	@Override
	public String sendApiPost(String reqData) throws Exception{
		String apiUrl = null;
		try {
			apiUrl = tokenService.getApiUrl();
		} catch (Exception e) {
			throw new MsgException(ResponseCodeEnum.UNKNOWN_ERROR,"获取访问路径异常："+e);
		}
		
		Header[] headers = null;
		try {
			headers = tokenService.getCarryTokenHeader();
		} catch (Exception e) {
			throw new MsgException(ResponseCodeEnum.GET_INTERFACEPLATFORM_TOKEN_HEAD_ERROR,"获取携带TOKEN的请求头异常："+e);
		}

		return HttpClientUtil.sendPostJson(apiUrl, reqData, headers);
	}

	@Override
	public String sendApiPostSaveLog(String reqData,Integer userId ,String type) throws Exception{
		String apiUrl = null;
		try {
			apiUrl = tokenService.getApiUrl();
		} catch (Exception e) {
			throw new MsgException(ResponseCodeEnum.UNKNOWN_ERROR,"获取访问路径异常："+e);
		}

		Header[] headers = null;
		try {
			headers = tokenService.getCarryTokenHeader();
		} catch (Exception e) {
			throw new MsgException(ResponseCodeEnum.GET_INTERFACEPLATFORM_TOKEN_HEAD_ERROR,"获取携带TOKEN的请求头异常："+e);
		}

		String result=HttpClientUtil.sendPostJson(apiUrl, reqData, headers);

		//存入日志
		MoreInfoLogEntity moreInfoLogEntity=new MoreInfoLogEntity();
		moreInfoLogEntity.setUserId(userId);
		moreInfoLogEntity.setType(type);
		moreInfoLogEntity.setAddTime(DateUtil.getNow());
		moreInfoLogEntity.setRequestData(reqData);
		moreInfoLogEntity.setResponseData(reqData);
		moreInfoLogDao.insertSelective(moreInfoLogEntity);

		return result;
	}
}
